//
//  RegisterDataVC.m
//  MovieDate
//
//  Created by ann on 16/3/14.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "RegisterDataVC.h"

@interface RegisterDataVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *borderStylePSWView;
@property (weak, nonatomic) IBOutlet UIView *borderStylePSWsecView;
@property (weak, nonatomic) IBOutlet UIView *borderStyleInviterTXF;
@property (weak, nonatomic) IBOutlet UIView *borderStyleNickNameTXF;
@property (weak, nonatomic) IBOutlet UIButton *manBTN;
@property (weak, nonatomic) IBOutlet UIButton *woManBTN;
@property (weak, nonatomic) IBOutlet UITextField *passWordTXF;

@property (weak, nonatomic) IBOutlet UITextField *secondPassWordTXF;

@property (weak, nonatomic) IBOutlet UITextField *nickNameTXF;
@property (weak, nonatomic) IBOutlet UITextField *inviterPhoneTXF;
@property (nonatomic, strong) NSString *sexIndex;

@end

@implementation RegisterDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBorderStylePSWView];
    [self initView];
    self.navigationController.navigationBarHidden = NO;
    // Do any additional setup after loading the view from its nib.
}

-(void)setBorderStylePSWView{
  
    self.borderStylePSWView.layer.borderWidth = 1;
    self.borderStylePSWView.layer.cornerRadius= 6.0;
    self.borderStylePSWsecView.layer.cornerRadius = 6.0;
    self.borderStylePSWsecView.layer.borderWidth = 1;
    self.borderStylePSWsecView.layer.cornerRadius= 6.0;
    self.borderStyleNickNameTXF.layer.cornerRadius = 6.0;
    self.borderStyleNickNameTXF.layer.borderWidth= 1;
    self.borderStyleInviterTXF.layer.borderWidth = 1;
    self.borderStyleInviterTXF.layer.cornerRadius= 6.0;
}
-(void)initView{
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"设置信息"];
    [self selAddLeftBackButton];
    
    [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man_c.png"] forState:(UIControlStateNormal)];
    [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man_c.png"] forState:(UIControlStateHighlighted)];
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman.png"] forState:(UIControlStateNormal)];
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman.png"] forState:(UIControlStateHighlighted)];
    [self.manBTN addTarget: self action:@selector(clickManBTN:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.manBTN  setSelected: YES];
    [self.woManBTN addTarget:self action:@selector(clickWomanBTN:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.woManBTN  setSelected: YES];
    
    self.secondPassWordTXF.delegate = self;
    
    [self setSexIndex:@"1"]; //设置用户默认性别

}

-(void)clickManBTN:(UIButton *)btn{
    
    [self setSexIndex:@"1"];
    
    [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man_c.png"] forState:(UIControlStateNormal)];
     [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man_c.png"] forState:(UIControlStateHighlighted)];
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman.png"] forState:(UIControlStateNormal)];
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman.png"] forState:(UIControlStateHighlighted)];

}
-(void)clickWomanBTN:(UIButton *)BTN{
    
    [self setSexIndex:@"2"];
    
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman_c.png"] forState:(UIControlStateNormal)];
    [self.woManBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_woman_c.png"] forState:(UIControlStateHighlighted)];
    [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man.png"] forState:(UIControlStateNormal)];
    [self.manBTN setBackgroundImage:[UIImage imageNamed:@"userRegister_man.png"] forState:(UIControlStateHighlighted)];

}

- (IBAction)nextBtnAction:(id)sender {
    if ([self.passWordTXF.text isEqualToString:self.secondPassWordTXF.text]) {
        __weak typeof(self) weakSelf = self;
        [SVProgressHUD show];
        [TDNetwork registerPostURL:registerIP andPhoneNumber:self.PhoneNumber andRandom:self.token andPwd:self.secondPassWordTXF.text andType:@"1"  andHeadImage:@"-1" andNickName:self.nickNameTXF.text andUserSex:self.sexIndex andUserAge:18 andMovieLabel:@"x" andInviterPhone:@"" andMac:@"" andImsi:@"" andSystemName:@"" andSystemVersion:@"" complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
            if (succeed) {
            
                switch ([ret intValue]) {
                    case 1:
                        [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                            [SVProgressHUD dismiss];
                        [weakSelf.navigationController popToRootViewControllerAnimated:NO];
                        break;
                    case -2:
                 
                        [SVProgressHUD showErrorWithStatus:@"随机数不对"];
                                 [SVProgressHUD dismiss];
                        break;
                    case -3:
              
                        [SVProgressHUD showErrorWithStatus:@"超时10分钟"];
                        [SVProgressHUD dismiss];
                        break;
                    default:
                        [SVProgressHUD showErrorWithStatus:@"未知错误"];
                        [SVProgressHUD dismiss];
                        break;
                }
            }else{
                [SVProgressHUD showInfoWithStatus:@"网络请求失败"];
                [SVProgressHUD dismiss];
            }
        }];
    }
}
#pragma -mark  UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([self.passWordTXF.text isEqualToString:self.secondPassWordTXF.text]) {
        
    }else{
        [SVProgressHUD showErrorWithStatus:@"前后密码不一样"];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
